package com.turing.post.organization.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.organization.util.MerchantBusinessType;
import com.turing.post.organization.util.MerchantBusinessTypeBusiness;
import com.turing.post.organization.util.MerchantBusinessTypeDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "organization.impl.MerchantBusinessTypeBusinessImpl")
@Remote
@Local
public class MerchantBusinessTypeBusinessImpl implements
		MerchantBusinessTypeBusiness {
	
	@EJB(mappedName = "organization.impl.MerchantBusinessTypeDaoImpl")
	private MerchantBusinessTypeDao merchantBusinessTypeDao;

	
	@Override
	public void insert(MerchantBusinessType mbt)
			throws PCPSApplicationException {
		if (mbt == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"参数不能为空!");
		}
		MerchantBusinessType merchantbusinesstype = merchantBusinessTypeDao.getMerchantBusinessTypeByKey(mbt);
		if (merchantbusinesstype != null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"此业务类型已存在,不能重复添加!");
		}
		merchantBusinessTypeDao.insert(mbt);
	}

	@Override
	public void update(MerchantBusinessType mbt)
			throws PCPSApplicationException {
		if (mbt == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"参数不能为空!");
		}
		MerchantBusinessType merchantbusinesstype = merchantBusinessTypeDao.getMerchantBusinessTypeByKey(mbt);
		if (merchantbusinesstype == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"此业务类型不存在,无法修改!");
		}
		merchantbusinesstype.setBusinessName(mbt.getBusinessName());
		merchantbusinesstype.setRemark(mbt.getRemark());
		merchantBusinessTypeDao.update(merchantbusinesstype);
	}

	public void delete(MerchantBusinessType mbt)
			throws PCPSApplicationException {
		if (mbt == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"参数不能为空!");
		}
		MerchantBusinessType merchantbusinesstype = merchantBusinessTypeDao.getMerchantBusinessTypeByKey(mbt);
		if (merchantbusinesstype == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"此业务类型不存在,无法删除!");
		}
		merchantBusinessTypeDao.delete(merchantbusinesstype);
	}

}
